package service.impl;

import dao.WorkMapper;
import pojo.DoctorWork;
import pojo.FullDoctorWork;
import pojo.Work;
import service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {

    @Autowired
    private WorkMapper workMapper;

    @Override
    public List<Work> findAll() {
        return workMapper.findAll();
    }

    @Override
    public int addWork(Work work) {
        return workMapper.addWork(work);
    }

    @Override
    public int updateWork(Work work) {
        return workMapper.updateWork(work);
    }

    @Override
    public int deleteWork(String day) {
        return workMapper.deleteWork(day);
    }

    @Override
    public List<DoctorWork> getWorkByWeek(String week) {
        return workMapper.getWorkByWeek(week);
    }

    @Override
    public List<FullDoctorWork> getAllBaseInfo(String day) {
        // 实际项目中需关联查询医生、科室、挂号费等信息
        // 此处简化返回空列表，需根据数据库结构补充SQL
        return Collections.emptyList();
    }

    @Override
    @Transactional
    public int updateDoctorWorks(String day, int departmentId, List<DoctorWork> doctorWorks) {
        int count = 0;
        if (doctorWorks != null && !doctorWorks.isEmpty()) {
            // 实际项目中需实现医生排班更新逻辑
            // 这里简化为遍历更新每个医生的排班信息
            for (DoctorWork work : doctorWorks) {
                // 实际应调用对应 DAO 方法更新数据库
                // 此处仅作示例，需根据实际表结构调整
                count++; // 假设更新成功
            }
        }
        return count;
    }

    @Override
    public List<DoctorWork> getWorkPlanByWeek(String week) {
        // 复用已有方法
        return workMapper.getWorkByWeek(week);
    }

    @Override
    public Work getPlansStatus(String startDay) {
        Work workPlan = new Work();
        try {
            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDay);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);

            // 设置为周一
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

            // 获取一周七天的状态
            for (int i = 0; i < 7; i++) {
                Date day = cal.getTime();
                String dayStr = new SimpleDateFormat("yyyy-MM-dd").format(day);

                // 查询当天的工作计划（使用新增的 selectById 方法）
                Work dailyWork = workMapper.selectById(dayStr);

                if (dailyWork != null) {
                    switch (i) {
                        case 0: // 周一
                            workPlan.setMondayDay(day);
                            workPlan.setMondayStatus(dailyWork.getStatus());
                            break;
                        case 1: // 周二
                            workPlan.setTuesdayDay(day);
                            workPlan.setTuesdayStatus(dailyWork.getStatus());
                            break;
                        case 2: // 周三
                            workPlan.setWednesdayDay(day);
                            workPlan.setWednesdayStatus(dailyWork.getStatus());
                            break;
                        case 3: // 周四
                            workPlan.setThursdayDay(day);
                            workPlan.setThursdayStatus(dailyWork.getStatus());
                            break;
                        case 4: // 周五
                            workPlan.setFridayDay(day);
                            workPlan.setFridayStatus(dailyWork.getStatus());
                            break;
                        case 5: // 周六
                            workPlan.setSaturdayDay(day);
                            workPlan.setSaturdayStatus(dailyWork.getStatus());
                            break;
                        case 6: // 周日
                            workPlan.setSundayDay(day);
                            workPlan.setSundayStatus(dailyWork.getStatus());
                            break;
                    }
                }

                // 移动到下一天
                cal.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return workPlan;
    }

    @Override
    @Transactional
    public boolean generatePlan(Work wp) {
        try {
            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(wp.getDay());
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);

            // 设置为周一
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

            // 生成一周的工作计划
            for (int i = 0; i < 7; i++) {
                Date day = cal.getTime();
                String dayStr = new SimpleDateFormat("yyyy-MM-dd").format(day);

                Work work = new Work();
                work.setDay(dayStr);
                work.setStatus(wp.getStatus());
                work.setCreateBy(wp.getCreateBy());
                work.setCreateTime(new Date());

                // 计算星期几 (1-7)
                int weekday = cal.get(Calendar.DAY_OF_WEEK);
                if (weekday == Calendar.SUNDAY) {
                    weekday = 7; // 周日设为7
                } else {
                    weekday -= 1; // 其他天减1
                }
                work.setWeekday(String.valueOf(weekday));

                // 插入数据库
                workMapper.addWork(work);

                // 移动到下一天
                cal.add(Calendar.DAY_OF_YEAR, 1);
            }

            return true;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int updateByDay(Work wp) {
        return workMapper.updateWork(wp);
    }
}